package cn.itcast.vo;


import lombok.Getter;
import lombok.Setter;
import java.util.List;

@Setter
@Getter
public class PageBean {
    //当前页码
    private int pageNum;
    //每页条数
    private int pageSize;

    //每页数据
    private List list;
    //总条数
    private Long total;

    //总页数
    private int pages;
    //上一页
    private int prePage;
    //下一页
    private int nextPage;
    //起始页码
    private int navigateFirstPage;
    //结束页码
    private int navigateLastPage;

    public PageBean(int pageNum, int pageSize, List list, Long total) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.list = list;
        this.total = total;

        //计算总页数 总条数/每页显示的条数
        //没有余数
        if (total%pageSize==0){
            this.pages=(int)(total/pageSize);
        }else {
            this.pages = ((int)(total/pageSize))+1;
        }

        //上一页
        //第一页的上一页还是第一页
        if (pageNum<=1){
            this.prePage=1;
        }else{
            this.prePage=pageNum-1;
        }

        //下一页
        //最后一页的下一页还是最后一页
        if (pageNum>=pages){
            this.nextPage=pages;
        }else{
            this.nextPage=pageNum+1;
        }

        //判断总页数pages是否大于5
        //1.pages<=5 startRow=1 endRow=pages
        if (pages<=5){
            this.navigateFirstPage=1;
            this.navigateLastPage=pages;
        }else{
            //2.pages>5
            //2.1 pageNum<=3 startRow=1  endRow=5
            if (pageNum<=3){
                this.navigateFirstPage=1;
                this.navigateLastPage=5;
            }else if ((pages-pageNum)<=2){
                //2.2 pageNum>3 pages-pageNum)<=2  startRow=pages-4 endRow=pages
                this.navigateFirstPage=pages-4;
                this.navigateLastPage=pages;
            }else {
                //2.2 pageNum>3   startRow=pages-2  endRow=pages+2
                this.navigateFirstPage=pages-2;
                this.navigateLastPage=pages+2;
            }
        }
    }

}
